<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px" style="width: 650px">
      <el-form-item label="头像" prop="user_cover">
        <el-upload
          class="avatar-uploader"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :limit="1"
        >
          <img v-if="form.user_cover" :src="form.user_cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户姓名" prop="user_name">
        <el-input type="text" v-model="form.user_name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="user_sex">
        <template>
          <el-radio v-model="form.user_sex" label="1">男</el-radio>
          <el-radio v-model="form.user_sex" label="0">女</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="联系电话" prop="user_phone">
        <el-input type="text" v-model="form.user_phone"></el-input>
      </el-form-item>
      <el-form-item label="个人简介" prop="user_text">
        <el-input type="textarea" v-model="form.user_text"></el-input>
      </el-form-item>
      <el-form-item label="所在城市" prop="city_name">
        <el-input type="text" v-model="form.city_name"></el-input>
      </el-form-item>

      <el-form-item label="学习驾照类型" prop="License_type">
        <el-checkbox-group v-model="form.License_type">
          <el-checkbox
            v-for="item in licensetypes"
            :key="item.license_type_id"
            :label="item.license_type"
            :value="item.license_type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateUser">修改用户</el-button>
        <el-button @click="resetform">重置</el-button>
        <el-button @click="cancel">取消返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import myaxios from "@/http/MyAxios";

import httpApi from "@/http";

export default {
  data() {
    return {
      form: {
        user_id: this.$route.params.id,
        user_name: "",
        user_sex: "",
        studytype: [],
        user_phone: "",
        License_type: [],
        user_cover: "",
        user_text:'',
      },
      licensetypes: [],
    };
  },
  methods: {
    resetform() {
      this.form.user_name = "";
      this.form.user_sex = "";
      this.form.studytype = [];
      this.form.user_phone = "";
      this.form.License_type = [];
      this.form.city_name = "";
      this.user_cover = "";
    },
    cancel() {
      this.$router.push("/home/user-list");
    },
    updateUser() {
      this.form.License_type = this.form.License_type.join("/");

      this.$refs["form"].validate((valid) => {
        if (valid) {
          httpApi.userApi.update(this.form).then((res) => {
            console.log("用户添加", res);
            if (res.data.code == 200) {
              this.$message.success("用户修改成功");
              this.$router.push("/home/user-list");
            } else {
              this.$message.error("修改失败请检查");
              this.form.License_type = this.form.License_type.split("/");
            }
          });
        }
      });
    },
    // 图片上传处理
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.form.user_cover = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传驾校照片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传驾校照片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    infolicensetype() {
      httpApi.commpleApi.queryLicensetype().then((res) => {
        console.log("驾照类型标签", res);
        this.licensetypes = res.data?.data?.result;
      });
    },
    infosubjiettype() {
      httpApi.commpleApi.querySubjectstype().then((res) => {
        console.log("科目类型标签", res);
        this.subjectstypes = res.data.data.result;
      });
    },
    queryById() {
      let params = {
        user_id: this.form.user_id,
      };
      httpApi.userApi.queryById(params).then((res) => {
        console.log("加载用户信息", res);
        this.form = res.data.data.result[0];
        this.form.License_type = this.form.License_type.split("/");
      });
    },
  },
  mounted() {
    this.infolicensetype();
    this.infosubjiettype();
    this.queryById();
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>